package com.mango.ch05;

import java.io.IOException;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import com.mango.HDFSTools.HDFSAPI;

/**
 * 反转排序demo 求词频
 * 
 * @author Mango
 *
 */
public class OR_WordJob {
	static Path outPath = new Path("/OR_WordJob/output");
	static Path inputPath = new Path("/OR_WordJob/input");

	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		// TODO Auto-generated method stub
		// 创建输入文件夹
		try {
			HDFSAPI hdfs = new HDFSAPI();
			hdfs.createDirectory(inputPath);
			// 先删除已经有的输出文件夹
			hdfs.delDirectory(outPath);
			// hdfs.orpOver();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			System.out.println("----------文件操作失败");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(OR_WordJob.class);
		job.setJobName("OR_WordJob");

		job.setPartitionerClass(OrderInversionPartitioner.class);

		job.setNumReduceTasks(3);

		job.setOutputKeyClass(PairOfWords.class);
		job.setOutputValueClass(DoubleWritable.class);

		job.setReducerClass(RelativeFrequencyReducer.class);
		job.setMapperClass(RelativeFrequencyMapper.class);
		job.setCombinerKeyGroupingComparatorClass(OR_Word_GroupComparator.class);

		job.setMapOutputKeyClass(PairOfWords.class);
		job.setMapOutputValueClass(IntWritable.class);

		FileOutputFormat.setOutputPath(job, outPath);
		FileInputFormat.addInputPath(job, inputPath);
		boolean status = job.waitForCompletion(true);
		System.exit(status ? 0 : 1);
	}

}
